#include "gridindex.h"

gridIndex::gridIndex()
{
    type=GRID;
    totalCol=10;
    totalRow=10;
}

gridIndex::~gridIndex()
{

}

bool gridIndex::initialize(double minX, double maxX, double minY, double maxY,geoMap*map1)
{
    this->map=map1;
    double maxLength;
    double xLength=maxX-minX;
    double yLength=maxY-minY;
    xLength>yLength?maxLength=xLength:maxLength=yLength;
    cellSize=maxLength/totalCol;

    //创建网格
    for(int row=0;row<totalRow;row++)
    {
        QVector<cellForIndex*> rowCells;

        for(int col=0;col<totalCol;col++)
        {
            cellForIndex* cell=new cellForIndex();
            cell->minX=col*cellSize+minX;
            cell->maxX=(col+1)*cellSize+minX;
            cell->minY=row*cellSize+minY;
            cell->maxY=(row+1)*cellSize+minY;
            rowCells.append(cell);
            leafCells.append(cell);
        }
        cells.append(rowCells);
    }
    conductIndex();
    isInitialized=true;
    return true;
}

QVector<cellForIndex*> gridIndex::getPaintCells()
{
    //网格索引的paintCells与leafCells等价
    return leafCells;
}

bool gridIndex::conductIndex()
{
    foreach(geoLayer*layer,map->geoLayers)
    {
        foreach(geoObject*object,layer->geoObjects)
        {
            foreach(QVector<cellForIndex* >rowCells,cells)
            {
                foreach(cellForIndex* cell,rowCells)
                {
                    cell->conductIndex(object);
                }
            }
        }
    }
    return true;
}
